<template>
  <div class="goods">
    <el-button type="success" plain @click="goGoodsAdd">添加商品</el-button>
    <!-- 表格 -->
    <el-table
    :data="goodsList"
    v-loading="isLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(255, 255, 255, 0.8)"
    >
      <el-table-column label="#" type="index" :index="indexMethods"></el-table-column>
      <el-table-column label="商品名称" prop="goods_name"></el-table-column>
      <el-table-column label="商品价格" prop="goods_price"></el-table-column>
      <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
      <el-table-column label="创建时间" prop="add_time">
        <template v-slot="{ row }">
          {{ row.add_time | timeFomat }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template>
          <el-button type="primary" plain icon="el-icon-edit" size="small"></el-button>
          <el-button type="danger" plain icon="el-icon-delete" size="small"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      Pnp: '',
      pagenum: 1,
      pagesize: 5,
      goodsList: [],
      total: 0,
      isLoading: false
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      this.isLoading = true
      const { meta, data } = await this.$axios.get('goods', {
        params: {
          Pnp: this.Pnp,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        // console.log(data)
        this.total = data.total
        this.goodsList = data.goods
      }
      this.isLoading = false
    },
    handleSizeChange (val) {
      this.pagenum = 1
      this.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      this.pagenum = val
      this.getGoodsList()
    },
    indexMethods (index) {
      // console.log(index + 1 + (this.pagenum - 1) * this.pagesize)
      return index + 1 + (this.pagenum - 1) * this.pagesize
    },
    goGoodsAdd () {
      this.$router.push('/goods-add')
    }
  }
}
</script>

<style lang="scss" scoped>
  .goods{
    .el-table{
      margin-top: 10px;
    }
  }
</style>
